﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace QuanLyThuoc
{
    class DanhSachChiTietHD
    {
        int n;
        static List<ChiTietHoaDon> DSCTHD;

        //khoi tao
        public DanhSachChiTietHD()
        {
            DSCTHD = new List<ChiTietHoaDon>();
        }

        //them moi chi tiet hoa don
        public void ThemDS()
        {
            Console.Write("\nSo luong them vao danh sach: ");
            while (int.TryParse(Console.ReadLine(), out n) == false)
            {
                Console.WriteLine("Nhap sai. Hay nhap lai!");
            }
            for (int i = 0; i < n; i++)
            {
                ChiTietHoaDon chiTiet = new ChiTietHoaDon();
                chiTiet.NhapCTHD();
                DSCTHD.Add(chiTiet);
                chiTiet.ghiFile();
            }
        }

        //tim theo ma hoa don
        public static List<ChiTietHoaDon> TimTheoMa(string maHD)
        {
            List<ChiTietHoaDon> list = new List<ChiTietHoaDon>();
            foreach (ChiTietHoaDon timMa in DSCTHD)
            {
                if (timMa.MaHoaDon == maHD)
                {
                    list.Add(timMa);
                }
            }
            return list;
        }

        /*public static List<ChiTietHoaDon> timTheoMa(string ma)
        {
            List<ChiTietHoaDon> chiTiet = new List<ChiTietHoaDon>();
            foreach (ChiTietHoaDon vtTim in DSCTHD)
            {
                if (ma == vtTim.MaHoaDon)
                {
                    chiTiet.Add(vtTim);
                }
            }
            return chiTiet;
        }*/

        //public bool SuaCTHD(string masua)
        //{
        //    List<ChiTietHoaDon> CTHDsua = TimTheoMa(masua);
        //    int x = -1;
        //    while (x != 0)
        //    {
        //            Console.WriteLine("\nChon doi tuong can sua: \n\t1: Ma thuoc \n\t2: So luong thuoc");
        //            while(int.TryParse(Console.ReadLine(),out x)==false)
        //            {
        //                Console.WriteLine("Nhap sai. Hay nhap lai!");
        //            }
        //            switch (x)
        //            { 
        //                case 1:
        //                    Console.Write("\nNhap ma thuoc sua: ");
        //                    sua.MaHoaDon = Console.ReadLine();
        //                    break;
        //                case 2:
        //                    Console.Write("\nNhap so luong sua: ");
        //                    sua.SoLuong = int.Parse(Console.ReadLine());
        //                    break;
        //            }
        //            return true;
        //    }
        //    return false;
        //}

        //public bool XoaCTHD(string maxoa)
        //{
        //    ChiTietHoaDon xoa = TimTheoMa(maxoa);
        //    if (xoa != null)
        //    {
        //        DSCTHD.Remove(xoa);
        //        n--;
        //        return true;
        //    }
        //    return false;
        //}

        public void XuatDsCTHD()
        {
            Console.Write("Nhap ma hoa don can xem: ");
            string mahd = Console.ReadLine();
            List<ChiTietHoaDon> ds = TimTheoMa(mahd);
            Console.WriteLine("\n\tMa HD \tTen Thuoc \tSo luong");
            foreach (ChiTietHoaDon chitiet in ds)
            {
                chitiet.XuatCTHD();
            }
            Console.WriteLine("\n----------------------------------------------");
        }

        //doc file
        public void docFile()
        {
            FileStream file = new FileStream("DSChitietHD.txt", FileMode.Open);
            BinaryReader br = new BinaryReader(file);
            while (br.PeekChar() != -1)
            {
                ChiTietHoaDon chitietHd = new ChiTietHoaDon();
                chitietHd.MaHoaDon = br.ReadString();
                chitietHd.MaThuoc = br.ReadString();
                chitietHd.SoLuong = int.Parse(br.ReadString());
                DSCTHD.Add(chitietHd);
            }
            br.Close();
            file.Close();
        }
    }
}
